package com.edu.demo.service.impl;

import com.edu.demo.entity.Department;
import com.edu.demo.exception.MyNullPointerException;
import com.edu.demo.mapper.DepartmentMapper;
import com.edu.demo.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 院系 服务实现类
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    /**
     * 批量导入院系
     *
     * @param file
     * @return
     */
    @Override
    public int insertDepartments(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        List<Department> departmentList = new ArrayList<>(sheet.getLastRowNum());
        for (int i = 1; i < sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if(Objects.isNull(row)){
                continue;
            }

            String name = row.getCell(0).getStringCellValue();
            if(Strings.isEmpty(name)){
                throw new MyNullPointerException("导入失败(第"+(i+1)+"行,院名称未填写)");
            }

            String code = row.getCell(1).getStringCellValue();
            if(Strings.isEmpty(code)){
                throw new MyNullPointerException("导入失败(第"+(i+1)+"行,院编号称未填写)");
            }


            Integer order =0;
            String stringCellValue = row.getCell(2).getStringCellValue();
            if(Strings.isEmpty(stringCellValue)){
                order = sheet.getLastRowNum();
            }
            order = Integer.parseInt(stringCellValue);

            String address = row.getCell(3).getStringCellValue();
            if(Strings.isEmpty(address)){
                address = "未知";
            }


            String charger = row.getCell(4).getStringCellValue();
            if(Strings.isEmpty(charger)){
                charger = "未知";
            }

            String intro = row.getCell(5).getStringCellValue();
            if(Strings.isEmpty(intro)){
                intro = "";
            }

            Department department = Department.builder()
                    .deptCode(code)
                    .deptName(name)
                    .sort(order)
                    .address(address)
                    .charger(charger)
                    .introduction(intro).build();


            departmentList.add(department);
        }
        saveBatch(departmentList);

        return 0;
    }
}
